#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define PRINTER_SIZE 5
#define FILE_SIZE 1000

typedef struct {
    int id;
    int priority;
} File;

typedef struct {
    File *file;
    int queueSize;
} Queue;

Queue printers[PRINTER_SIZE];
int compare(const void *a, const void *b);
void addFile(int, int, int);
int rmFile(int);

int main() {
    for(int i = 0; i < PRINTER_SIZE; i++) {
        printers[i].queueSize = 0;
        printers[i].file = (File *)malloc(sizeof(File) * FILE_SIZE);
    }
    int opCount;
    scanf("%d", &opCount); 
    int fileCount = 0;
    for(int i = 0; i < opCount; i++) {
        char operation[4];
        scanf("%s", operation);
        int printerIndex;
        scanf("%d", &printerIndex);
        printerIndex--;
        if (!strcmp(operation, "IN")) {
            int priority;
            // error 1# 只有 IN 操作时，才接收三个输入
            scanf("%d", &priority);
            fileCount++;
            addFile(printerIndex, fileCount, priority);
        }
        else if(!strcmp(operation, "OUT")) {
            int rmFileId = rmFile(printerIndex);
            if (rmFileId == -1) {
                printf("NULL\n");
            }
            else {
                printf("%d\n", rmFileId);
            }
        }
        
    }
    return 0;
}

int compare(const void *a, const void *b) {
    File file1 = *(const File *)a;
    File file2 = *(const File *)b;
    if (file1.priority != file2.priority) {
        return file2.priority - file1.priority;
    }
    else {
        // error 2# id 小的优先级应该更高
        return file1.id - file2.id;
    }
}

void addFile(int printerIndex, int fileCount, int priority) {
    int fileIndex = printers[printerIndex].queueSize;
    printers[printerIndex].file[fileIndex].id = fileCount;
    printers[printerIndex].file[fileIndex].priority = priority;
    (printers[printerIndex].queueSize)++;

    int queueSize = printers[printerIndex].queueSize;
    qsort(printers[printerIndex].file, queueSize, sizeof(File), compare);
}

int rmFile(int printerIndex) {
    int curSize = printers[printerIndex].queueSize;
    if(curSize == 0) {
        return -1;
    }
    int rmId = printers[printerIndex].file[0].id;
    for(int i = 0; i < curSize-1; i++) {
        printers[printerIndex].file[i] = 
        printers[printerIndex].file[i+1];
    }
    printers[printerIndex].queueSize--;
    return rmId;
}
